//
// Created by Administrator on 2021/11/26.
//

#include "Display.h"

Display *Display::currentInstance = nullptr;

Display::Display(Splatting *s, int argc, char **argv) : splatting(s) {
    glutInit(&argc, argv);// GLUT环境初始化
    glutInitWindowPosition(0, 0);// 定义窗口位置
    glutInitWindowSize(iWidth, iHeight);// 定义窗口大小
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);// 显示模式初始化
    glutCreateWindow("Splatting");// 显示窗口，窗口标题为splatting
    glOrtho(0, 520, 0, 520, -520, 0);// 正射投影
    // 清理窗口
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_BLEND);// enable or disable server-side GL capabilities
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// specify pixel arithmetic
    glShadeModel(GL_FLAT);// select flat or smooth shading
    glPointSize(1);// specify the diameter of rasterized points
}

void Display::exec() {
    setup_display_callback();
    glutMainLoop(); // 进入GLUT消息循环，开始执行程序
}

void Display::display() {
    float r, g, b, a;

    for (int i = VOLUME_X; i > 0; i--) {
        splatting->reset_value();
        splatting->exec(i);

        auto value = splatting->get_value();

        for (int x = 0; x < 512; x++) {
            for (int y = 0; y < 512; y++) {
                auto v = value[x * 512 + y];
                if (v > 300) {
                    glColor4f(0.3, 0.9, 0.4, 1);
                    glRectf(400, 400, 500, 500);
                }
                if (v > 170) {
                    r = (float) (v + 50) / 255;
                    g = (float) (v + 50) / 255;
                    b = (float) (v + 50) / 255;
                    a = 0.99;
                }

                if (v > 50 && v < 170) {
                    r = (float) (v + 30) / 255;
                    g = (float) 20 / 255;
                    b = (float) 20 / 255;
                    a = 0.1;
                }
                if (v < 50) {
                    r = 0;
                    g = 0;
                    b = 0;
                    a = 0;
                }

                glColor4f(r, g, b, a);
                glBegin(GL_POINTS);
                glVertex2f((float) (512 - x), (float) (512 - y));
                glEnd();
            }
        }
        glutSwapBuffers();
    }
}
